﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Diagnostics;


namespace Amarok
{
	/// <summary>
	/// This type implements a decorator around <see cref="System.IProgress{T}"/> that constrains the number 
	/// of progress updates based on a configurable minimum time interval between two progress updates.
	/// </summary>
	public sealed class IntervalConstrainedProgress<T> :
		ILazyProgress<T>
	{
		// data
		private readonly Stopwatch mWatch = new Stopwatch();


		#region ++ IProgress<T> Interface ++

		/// <summary>
		/// Reports a progress update.
		/// </summary>
		/// 
		/// <param name="value">
		/// The value of the updated progress.</param>
		public void Report(T value)
		{
			if (mWatch.IsRunning && mWatch.Elapsed < this.MinimumInterval)
				return;

			this.Progress.Report(value);

			mWatch.Restart();
		}

		#endregion

		#region ++ ILazyProgress<T> Interface ++

		/// <summary>
		/// Reports a progress update.
		/// </summary>
		/// 
		/// <param name="valueFunc">
		/// A function that returns the value of the updated progress.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public void Report(Func<T> valueFunc)
		{
			Verify.NotNull(valueFunc, "valueFunc");

			if (mWatch.IsRunning && mWatch.Elapsed < this.MinimumInterval)
				return;

			this.Progress.Report(valueFunc());

			mWatch.Restart();
		}

		#endregion

		#region ++ Public Interface ++

#pragma warning disable 3003
		/// <summary>
		/// Gets a reference to the inner <see cref="System.IProgress{T}"/>.
		/// </summary>
		public IProgress<T> Progress
		{
			get;
			private set;
		}
#pragma warning restore 3003

		/// <summary>
		/// Gets the configured minimum interval between two progress updates.
		/// </summary>
		public TimeSpan MinimumInterval
		{
			get;
			private set;
		}


#pragma warning disable 3001
		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// 
		/// <param name="progress">
		/// A reference to the inner <see cref="System.IProgress{T}"/>.</param>
		/// <param name="minimumInterval">
		/// The minimum interval between two progress updates.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// An empty string was passed to a method that did not accept it as a valid argument.</exception>
		public IntervalConstrainedProgress(IProgress<T> progress, TimeSpan minimumInterval)
		{
			Verify.NotNull(progress, "progress");
			Verify.NotNegative(minimumInterval, "minimumInterval");

			this.Progress = progress;
			this.MinimumInterval = minimumInterval;
		}
#pragma warning restore 3001

		#endregion

	}
}
